import {swfobject} from '../../common/swfobject'
import EventEmitter from '../../common/EventEmitter'
export default class VideoSwf{
	constructor(swf_param,div_id) {
	  this.swf_param = swf_param
		this.div_id = div_id
		this.init()
	}
	init(){
		this.emitter = new EventEmitter()
		this.callbackFn = this.Created_cb.bind(this)
		var swfVersionStr = "11.0.0";
		// To use express install, set to playerProductInstall.swf, otherwise the empty string. 
		var xiSwfUrlStr = "playerProductInstall.swf";
		var flashvars = {};
		
		flashvars = {
			url:this.swf_param.url,
			ext_statistics:1,
			public_cdn:1,
			gid:this.swf_param.room_id,
			uid:this.swf_param.uid,
			level:this.swf_param.level,
			token:this.swf_param.token,
			nickname:this.swf_param.nickname,
			splash:this.swf_param.splash,
			bps : this.swf_param.bps,
			fps : this.swf_param.fps,
			vq : this.swf_param.vq,
			src_w:this.swf_param.src_w,
			src_h:this.swf_param.src_h,
			hd:this.swf_param.is_hd,
			obs:this.swf_param.obs,
			vu:1,
			speex:0,
			hdassist:0,
			camera_wl:this.swf_param.camera_wl,
			level:this.swf_param.level
		};
		var params = {};
		params.quality = "high";
		params.bgcolor = "#57799c";
		params.allowscriptaccess = "sameDomain";
		params.allowfullscreen = "true";
		params.wmode = "transparent";
		var attributes = {};
		attributes.id = this.swf_param.swfName;
		attributes.name = this.swf_param.swfName;
		attributes.align = "middle";
		this.swf_name = this.swf_param.swfName
		swfobject.embedSWF(
			this.swf_param.swfFile, this.div_id,
			'100%', '100%',
			swfVersionStr, xiSwfUrlStr, 
			flashvars, params, attributes,this.callbackFn);
		this.object = document.getElementById(this.swf_param.swfName)
	}
	Created_cb(cbobj){
		if (cbobj.success){
			this.swf_ready = 1;
		}
		else{
			alert('flash初始化失败，请安装插件，如果您确定已经有flash插件，请更换浏览器尝试或者联系客服')
		}
	}
	LiveState(state){
		this.live = state
		if (this.swf_name !== 'recv'){
			return;
		}
		swfobject.getObjectById("recv").LiveState(state)
	}
	Logout(){
		if (this.swf_name !== 'recv'){
			return
		}
		swfobject.getObjectById("recv").Logout()
	}
	destroy(){
		let newNodeStr = '<div id="'+this.div_id+'"></div>'
		let parentNode = this.object.parentNode
		parentNode.removeChild(this.object)
		parentNode.insertAdjacentHTML('beforeEnd',newNodeStr)
	}
	on(event, listener) {
		this.emitter.on(event, listener)
	}
	off(event, listener) {
		this.emitter.off(event, listener)
	}
}